<?php

namespace addons\fzdc\controller;

use app\admin\model\fzdc\message\Push;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use think\Config;
use think\Log;

class Common extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function common()
    {
        $fzdc = get_addon_info('fzdc');
        if (!$fzdc || $fzdc['state'] == 0) {
            $this->error("请先启用插件");
        }
        $config = get_addon_config("fzdc");
        $data = [
            "timeout" => $config['timeout'],        //订单超时时间
            "default_points" => $config['default_points'], //默认支付返积分
            "appid" => $config['appid'],          //微信小程序appid
            "secret" => $config['secret'],         //微信小程序secret
            "mchid" => $config['mchid'],          //微信小程序商户号
            "points" => $config['points'],         //签到获得积分
            "addup" => $config['addup'],          //累计签到递增积分
            "max" => $config['max'],            //连续签到最大天数
            "tel" => $config['tel'],            //加盟咨询
            "pact" => 3,           //会员协议id
            "avatar" => cdnurl("/assets/img/avatar.png", true),           //默认头像
            "username" => "未登录",           //默认名字
            "score" => 0,           //默认积分
            "upgrade" => 0,           //默认积分
            "level" => 0,           //默认积分
            "img" => cdnurl($config['img'], true),           //分享图片
            "share_title" => $config['share_title'],           //分享标题
            "share_route" => "/pages/index/index",           //分享路径
            "con_img" => cdnurl($config['con_img'], true),           //领券中心页面
            "iszq" => $config['iszq'],
            "iswm" => $config['iswm'],
            "isyy" => $config['isyy'],
            "ists" => $config['ists'],
            "isqb" => $config['isqb'],
            "ispayh" => $config['ispayh'],
            "app_filings" => $config['app_filings'],            //APP备案号
            "mini_filings" => $config['mini_filings'],            //小程序备案号
            "business_license" => $config['business_license'],            //经营许可
            "refund_switch" => $config['refund_switch'],
            "cwf" => $config['cwf'],
            "iscwf" => $config['iscwf'],
            "isnews" => $config['isnews'],
            "login_type" => $config['login_type'],
			"tx_type" => explode(",", $config['tx_type']),
            "is_shlogin" => $config['is_shlogin'],


        ];
        $data['message'] = Push::where("masterplate", "<>", "")->column("masterplate");
        $data['jc_message'] = Push::where("type", 4)->column("masterplate");
        $this->success("获取成功", $data);
    }


    /**
     * 上传文件
     * @ApiMethod (POST)
     * @param File $file 文件流
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
        }

    }
}